#pragma once

#include <afxinet.h>

class CHttpHelper
{
public:
	CHttpHelper(const CString &strServer, WORD nPort = 80);
	~CHttpHelper(void);

	template<typename CallbackT>
	BOOL Get(const CString &strObjectName, CallbackT &cb);

private:
	CInternetSession session;
	CHttpConnection *pServer;
};

template<typename CallbackT>
BOOL CHttpHelper::Get(const CString &strObjectName, CallbackT &cb)
{
	BOOL bRet = FALSE;

	CHttpFile *pFile = NULL;

	try
	{
		pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObjectName);
		pFile-> SendRequest();

		DWORD dwRet;
		pFile-> QueryInfoStatusCode(dwRet);
		if(dwRet == HTTP_STATUS_OK)
		{
			char buffer[1024];
			DWORD nRead = pFile->Read(buffer, sizeof(buffer));
			while(nRead > 0)
			{
				cb(buffer, nRead);
				nRead = pFile->Read(buffer, sizeof(buffer));
			}

			bRet = TRUE;
		}
	}
	catch(CInternetException*)
	{
		//pEx->GetErrorMessage((LPTSTR)pBuffer, nSize);
	}

	if(pFile)
	{
		delete pFile;
		pFile = NULL;
	}
	return bRet;
}